package com.grantz.function.bbs.core.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.grantz.function.bbs.core.entity.GroupUser;
import com.grantz.function.bbs.core.inter.GroupUserService;
import com.grantz.function.bbs.core.jpa.GroupUserJPA;
import com.grantz.model.bbs.GroupUserModel;
import com.grantz.model.bbs.GroupUserSearch;
import com.grantz.models.base.element.page.ElementPage;

/**
 * 文件名称： com.grantz.function.bbs.core.impl.GroupUserServiceImpl.java</br>
 * 初始作者： WenBin</br>
 * 创建日期： 2020年2月13日</br>
 * 功能说明： 这里用一句话描述这个类的作用--此句话需删除 <br/>
 *
 * =================================================<br/>
 * 修改记录：<br/>
 * 修改作者        日期       修改内容<br/>
 *
 *
 * ================================================<br/>
 *  Copyright (c) 2010-2011 .All rights reserved.<br/>
 */
@Service
public class GroupUserServiceImpl implements GroupUserService {

	@Autowired
	private  GroupUserJPA dao;

	@Override
	public GroupUser save(GroupUser entity) {
		return dao.save(entity);
	}

	@Override
	public ElementPage<GroupUserModel> queryElementPage(GroupUserSearch query) throws Exception {
		Page<GroupUser>  page = queryPage(query);
		return new ElementPage<GroupUserModel>(popGroupUserModel(page.getContent()), page.getTotalElements(), page.getNumber(), page.getSize());
	}

	private  List<GroupUserModel>  popGroupUserModel(List<GroupUser>  entityList){
		List<GroupUserModel>  rt = new ArrayList<GroupUserModel>();
		GroupUserModel  model = null ;
		for(GroupUser  entity : entityList) {
			  model =  new GroupUserModel();
			  model.setGroupId(entity.getGroupId());
			  model.setGroupRole(entity.getGroupRole());
			  model.setId(entity.getId());
			  model.setSsoId(entity.getSsoId());
			  model.setUserLevel(entity.getUserLevel());
			  rt.add(model);
		}
		return rt;
	}

	@Override
	public Page<GroupUser> queryPage(GroupUserSearch model) throws Exception {
		PageRequest pageable = PageRequest.of(model.getPageNo(), model.getPageSize());
		Specification<GroupUser> specifications = new Specification<GroupUser>() {

			private static final long serialVersionUID = 1L;

			@Override
			public Predicate toPredicate(Root<GroupUser> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				List<Predicate> list = new ArrayList<Predicate>();
//				if(StringUtil.isNotEmpty(model.getWechatno())) {
//					Predicate pred = criteriaBuilder.like(root.get("wechatNo").as(String.class), "%"+model.getWechatno()+"%");
//					list.add(pred);
//				}
				Predicate[] p = new Predicate[list.size()];
				return criteriaBuilder.and(list.toArray(p));
			}
		};
		return dao.findAll(specifications, pageable);
	}

	@Override
	public Optional<GroupUser> findOneById(String id) {
		return dao.findById(id);
	}

}
